<?php
class AdminAction extends BaseAction{
	public function addAdmin(){
		$this->assign("position","管理员添加");
		if($this->isPost()){
			$data['admin_name'] = addslashes(trim($_POST['a_name']));
			$data['admin_pwd'] = md5(addslashes(trim($_POST['a_password'])));
			//var_dump($data);
			if (M("admin")->where("admin_name='".$data['admin_name']."'")->find()){
				$this->assign("jumpUrl","__URL__/addAdmin");
				$this->assign("waitSecond",3);
				$this->error("管理员已存在！！！");
			}else {
				$result = M("admin")->add($data);
				if($result){
					$this->assign("jumpUrl","__URL__/delAdmin");
					$this->assign("waitSecond",3);
					$this->success("管理员添加成功！！！");
				}else {
					$this->assign("jumpUrl","__URL__/addAdmin");
					$this->assign("waitSecond",3);
					$this->error("管理员添加失败！！！");
				}
			}
			
		}else{
			$this->display();
		}
	}
	
	public function delAdmin(){
		$this->assign("position","管理员删除");
		if($this->isPost()){
			
			if ( M("adminpower")->where("admin_id=".$_POST['admin_id'])->find() ){
				M("admin")->startTrans();
				$result = M("admin")->where("id=".$_POST['admin_id'])->delete();
				$delAdminPower = M("adminpower")->where("admin_id=".$_POST['admin_id'])->delete();
				//echo M("admin")->getLastSql();die;
				if ( $delAdminPower && $result ){
					M("admin")->commit();
					echo "success";
				}else {
					M("admin")->rollback();
					echo "failure";
				}
			} else {
				$result = M("admin")->where("id=".$_POST['admin_id'])->delete();
				if ($result){
					M("admin")->commit();
					echo "success";
				}else {
					M("admin")->rollback();
					echo "failure";
				}
			}
		}else{
			$allAdmin = M("admin")->field("id,admin_name")->where("admin_name != 'vinnie' and id != 1 and admin_name !='".$_SESSION['administer']['name']."'")->select();
			$this->assign("admin",$allAdmin);
			//var_dump($allAdmin);
			$this->display();
		}
		
	}
	
}